//package cc.vv.baselibrary.activity.deliveryAddress;
//
//import android.content.Intent;
//import android.os.Bundle;
//import android.support.annotation.Nullable;
//import android.text.TextUtils;
//import android.view.View;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//
//import java.util.ArrayList;
//
//import cc.vv.baselibrary.R;
//import cc.vv.baselibrary.activity.BaseActivity;
//import cc.vv.baselibrary.adapter.deliveryAddress.SelectedDeliveryAddressAdapter;
//import cc.vv.baselibrary.api.BaseLibNetworkInterface;
//import cc.vv.baselibrary.bean.DeliveryAddress.CommonDeliveryAddressBean;
//import cc.vv.baselibrary.bean.DeliveryAddress.request.CommonDeliveryAddressRequestObj;
//import cc.vv.baselibrary.bean.DeliveryAddress.response.GetDeliveryAddressResponseObj;
//import cc.vv.baselibrary.bean.PublicViewHolder;
//import cc.vv.baselibrary.bean.base.BaseResponseObj;
//import cc.vv.baselibrary.db.daTable.DeliveryAddressBean;
//import cc.vv.baselibrary.global.BCIntentKey;
//import cc.vv.baselibrary.http.BaseHttpRequest;
//import cc.vv.baselibrary.server.DeliveryAddressRelatedServer;
//import cc.vv.baselibrary.util.BCColorSetBackgroundUtil;
//import cc.vv.baselibrary.util.BCObjectProcessingUtil;
//import cc.vv.baselibrary.util.BaseResourceUtil;
//import cc.vv.baselibrary.util.ColorUtil;
//import cc.vv.baselibrary.util.LKLogUtil;
//import cc.vv.baselibrary.util.NetworkEnvironmentMonitoringUtil;
//import cc.vv.baselibrary.view.BaseTopBarView;
//
//
///**
// * 选择收货地址界面
// * Created by sun.li on 2017/7/11.
// *
// * @author sun.li
// */
//public class SelectDeliveryAddressActivity extends BaseActivity {
//
//    /**
//     * 选择收货地址界面标志
//     */
//    public static final String SelectDeliveryAddressActivityLogo = "SelectDeliveryAddressActivityLogo";
//
//    /**
//     * 选择界面跳转新界面Code
//     */
//    public static final int SelectDeliveryAddressCode = 1001;
//
//    /**
//     * 地址选择标志Key
//     */
//    public static final String AddressSelectLogoKey = "AddressSelectLogoKey";
//
//
//    /**
//     * 收货地址列表适配器对象
//     */
//    private SelectedDeliveryAddressAdapter deliveryAddressAdapter;
//
//    /**
//     * 收货地址对象集合
//     */
//    private ArrayList<DeliveryAddressBean> deliveryAddressBeens;
//    /**
//     * 当前选中的地址ID
//     */
//    private String selectedAddressId;
//
//    private ViewHolder viewHolder;
//
//    private class ViewHolder extends PublicViewHolder {
//        /**
//         * 收货地址列表控件
//         */
//        private ListView lv_sdaa_address_list;
//
//        /**
//         * 收货地址列表无数据展示控件
//         */
//        private LinearLayout lv_sdaa_no_address_layout;
//        /**
//         * 标题栏标题
//         */
//        private BaseTopBarView jtb_sdaa_top_bar;
//
//        LinearLayout ll_sdaa_add_delivery_address;
//
//
//    }
//
//    @Override
//    protected void initView(@Nullable Bundle bundle) {
//        super.initView(bundle);
//        BCColorSetBackgroundUtil.setBackgroundMainColor(viewHolder.ll_sdaa_add_delivery_address);
//    }
//
//    @Override
//    protected PublicViewHolder initViewHolderObject() {
//        viewHolder = new ViewHolder();
//        return viewHolder;
//    }
//
//    @Override
//    protected int initLayoutRes() {
//        return R.layout.activity_select_delivery_address;
//    }
//
//
//    @Override
//    protected void baseOnClick(int viewId) {
//
//        if (BaseResourceUtil.whetherResourceIDSame(viewId, R.id.ll_sdaa_add_delivery_address)) {
//            //新增收货地址按钮
//            Intent intent = new Intent(this, AddOrEditorDeliveryAddressActivity.class);
//            intent.putExtra(AddOrEditorDeliveryAddressActivity.ManageOrSelectSkipLogoKey, SelectDeliveryAddressActivityLogo);
//            startActivityForResult(intent, SelectDeliveryAddressCode);
//        }
//
//    }
//
//
//    @Override
//    protected void initData(@Nullable Bundle data) {
//        super.initData(data);
//        deliveryAddressBeens = new ArrayList<>();
//        if (NetworkEnvironmentMonitoringUtil.isNetworkConnected(this)) {
//            //有网
//            getAddress();
//        } else {
//            //没网
//            DeliveryAddressRelatedServer.getInstance().deleteUnqualifiedDeliveryAddressInfo();
//            deliveryAddressBeens = DeliveryAddressRelatedServer.getInstance().getDeliveryAddressList();
//        }
//
//        try {
//            selectedAddressId = getIntent().getStringExtra(BCIntentKey.CurrentAddressSelectID);
//        } catch (Exception e) {
//            LKLogUtil.e(e.getMessage(), e);
//            selectedAddressId = "";
//        }
//
//    }
//
//
//    @Override
//    protected void initAction(@Nullable Bundle data) {
//        super.initAction(data);
//        deliveryAddressAdapter = new SelectedDeliveryAddressAdapter(this, deliveryAddressBeens);
//        viewHolder.lv_sdaa_address_list.setAdapter(deliveryAddressAdapter);
//        refreshAdapter();
//
//        viewHolder.jtb_sdaa_top_bar.setEventInterface(new BaseTopBarView.EventInterface() {
//            //返回按钮
//            @Override
//            public void leftOnClick() {
//                finish();
//            }
//
//            @Override
//            public void leftCloseClick() {
//
//            }
//
//            //标题栏右侧按钮，执行编辑功能,跳转管理收货地址界面
//            @Override
//            public void rightOnClick() {
//                startActivity(new Intent(SelectDeliveryAddressActivity.this,
//                        ManageDeliveryAddressActivity.class));
//            }
//        });
//    }
//
//
//    @Override
//    protected void onResume() {
//        super.onResume();
////        refreshAdapter();
//        getAddress();
//    }
//
//    /**
//     * 刷新列表适配器
//     */
//    public void refreshAdapter() {
//        try {
//            deliveryAddressBeens = DeliveryAddressRelatedServer.getInstance().getDeliveryAddressList();
//            if (!TextUtils.isEmpty(selectedAddressId)) {
//                if (deliveryAddressBeens != null && deliveryAddressBeens.size() > 0) {
//                    for (int i = 0; i < deliveryAddressBeens.size(); i++) {
//                        if (deliveryAddressBeens.get(i).getId().equals(selectedAddressId)) {
//                            deliveryAddressBeens.get(i).setSelected(true);
//                        } else {
//                            deliveryAddressBeens.get(i).setSelected(false);
//                        }
//                    }
//                }
//            }
//            if (deliveryAddressBeens.size() <= 0) {
//                viewHolder.lv_sdaa_no_address_layout.setVisibility(View.VISIBLE);
//                viewHolder.lv_sdaa_address_list.bringToFront();
//            } else {
//                viewHolder.lv_sdaa_no_address_layout.setVisibility(View.GONE);
//            }
//            deliveryAddressAdapter.clear();
//            deliveryAddressAdapter.addAll(deliveryAddressBeens);
//            deliveryAddressAdapter.notifyDataSetChanged();
//            if (deliveryAddressBeens != null && deliveryAddressBeens.size() > 0) {
//                viewHolder.jtb_sdaa_top_bar.getRightTV().setTextColor(ColorUtil.setBCColor(R.color.color_28BFA9));
//                viewHolder.jtb_sdaa_top_bar.getRl_ljtb_right_layout().setEnabled(true);
//            } else {
//                viewHolder.jtb_sdaa_top_bar.getRightTV().setTextColor(ColorUtil.setBCColor(R.color.color_DDDDDD));
//                viewHolder.jtb_sdaa_top_bar.getRl_ljtb_right_layout().setEnabled(false);
//            }
//        } catch (Exception e) {
//            LKLogUtil.e(e.getMessage(), e);
//        }
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == AddOrEditorDeliveryAddressActivity.AddOrEditorDeliveryAddressCode) {
//            switch (requestCode) {
//                case SelectDeliveryAddressCode:
//                    //选择收货地址新增界面回调
//                    DeliveryAddressBean deliveryAddressBean = null;
//                    if (data != null) {
//                        try {
//                            deliveryAddressBean = (DeliveryAddressBean) data.getExtras().get(AddOrEditorDeliveryAddressActivity.AddressAddLogoKey);
//                        } catch (Exception e) {
//                            LKLogUtil.e(e.getMessage(), e);
//                        }
//                    }
//                    Intent intent = new Intent();
//                    if (deliveryAddressBean != null) {
//                        intent.putExtra(AddressSelectLogoKey, deliveryAddressBean);
//                    }
//                    setResult(SelectDeliveryAddressCode, intent);
//                    finish();
//                    break;
//                default:
//                    break;
//            }
//        }
//    }
//
//    /**
//     * 查询收货地址
//     */
//    private void getAddress() {
//        CommonDeliveryAddressRequestObj commonDeliveryAddressRequestObj = new CommonDeliveryAddressRequestObj();
//        //此处需要确定已经登录了，才可以访问该接口（默认的userid和token不一定登录过，这里使用某个已登录的数据，全项目运行时不需要）
//        BaseHttpRequest.postRequest(BaseLibNetworkInterface.GET_ADDRESS, commonDeliveryAddressRequestObj,
//                GetDeliveryAddressResponseObj.class, true);
//    }
//
//    @Override
//    protected void getData(BaseResponseObj obj) {
//        super.getData(obj);
//        //查询收货地址
//        if (obj instanceof GetDeliveryAddressResponseObj) {
//            ArrayList<CommonDeliveryAddressBean> datas = BCObjectProcessingUtil.objMandatoryConversionClass(obj.data, new ArrayList<CommonDeliveryAddressBean>());
//            if (datas != null && datas.size() > 0) {
//                ArrayList<DeliveryAddressBean> cashDatas = new ArrayList<>();
//                for (CommonDeliveryAddressBean bean : datas) {
//                    DeliveryAddressBean cashBean = new DeliveryAddressBean();
//                    cashBean.setId(bean.getId());
//                    cashBean.setUserName(bean.getAddressName());
//                    cashBean.setPhone(bean.getAddressPhone());
//                    //省
//                    String addressProvince = bean.getAddressProvince();
//                    //市
//                    String addressCity = bean.getAddressCity();
//                    //区县
//                    String addressArea = bean.getAddressArea();
//                    String simple_address = addressProvince + " " + addressCity + " " + addressArea;
//                    cashBean.setCity(simple_address);
//                    cashBean.setAddress(bean.getAddressDetailed());
//                    //默认地址  0默认，1非默认
//                    if ("0".equals(bean.getAddressDefaultChoose())) {
//                        cashBean.setDefaultAddress(true);
//                    } else {
//                        cashBean.setDefaultAddress(false);
//                    }
//                    if (!TextUtils.isEmpty(bean.getAddressTag())) {
//                        cashBean.setLabel(bean.getAddressTag());
//                    }
//                    cashDatas.add(cashBean);
//                }
//                deliveryAddressBeens.clear();
//                deliveryAddressBeens.addAll(cashDatas);
//                for (DeliveryAddressBean bean : deliveryAddressBeens) {
//                    LKLogUtil.e("=====" + bean.getLabel());
//                }
//                //数据同步
//                DeliveryAddressRelatedServer.getInstance().deleteAllDeliveryAddressInfo();
//                DeliveryAddressRelatedServer.getInstance().updateDeliveryAddressInfos(deliveryAddressBeens);
//            } else {
//                //数据同步
//                DeliveryAddressRelatedServer.getInstance().deleteAllDeliveryAddressInfo();
//            }
//            //界面刷新
//            refreshAdapter();
//        }
//    }
//
//
//}
